package com.leoren.run.activity.announcement;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.leoren.run.R;
import com.leoren.run.common.Const;
import com.leoren.run.utils.GsonUtil;
import com.leoren.run.vo.AnnounceVo;

import java.util.List;

/**
 * 系统公布列表适配器
 */
public class AnnounceAdapter extends RecyclerView.Adapter<AnnounceAdapter.AnnounceHolder> {

    private Context context;
    private List<AnnounceVo> announceVos;

    public AnnounceAdapter(Context context, List<AnnounceVo> announceVos) {
        this.context = context;
        this.announceVos = announceVos;
    }

    static class AnnounceHolder extends RecyclerView.ViewHolder{

        LinearLayout itemAnnounceView;
        TextView titleText;
        TextView contentText;
        TextView dateText;

        public AnnounceHolder(@NonNull View itemView) {
            super(itemView);
            itemAnnounceView = (LinearLayout) itemView;
            titleText = itemView.findViewById(R.id.item_announce_title);
            contentText = itemView.findViewById(R.id.item_announce_content);
            dateText = itemView.findViewById(R.id.item_announce_time);

        }
    }

    @NonNull
    @Override
    public AnnounceHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_announce_layout, viewGroup, false);
        AnnounceHolder holder = new AnnounceHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull AnnounceHolder holder, final int i) {
        final AnnounceVo vo = announceVos.get(i);
        holder.dateText.setText(vo.getDate());
        holder.titleText.setText(vo.getTitle());
        holder.contentText.setText(vo.getContent());

        holder.itemAnnounceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AnnounceActivity.class);
                intent.putExtra(Const.ANNOUNCE, GsonUtil.GenerateJsonStr(vo));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return announceVos.size();
    }
}
